package com.hui.dbmigrator.handler;

import com.hui.dbmigrator.config.DatabaseConfig;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

// AbstractDatabaseHandler.java
public abstract class AbstractDatabaseHandler implements DatabaseHandler {

    protected String quoteIdentifier(String identifier) {
        return identifier; // Default no quoting
    }

    protected String formatDefaultValue(Object value, String columnType) {
        if (value == null) return "";

        if (columnType.startsWith("VARCHAR") || columnType.startsWith("CHAR") ||
                columnType.startsWith("TEXT")) {
            return "'" + value.toString().replace("'", "''") + "'";
        }
        return value.toString();
    }

    protected Connection getConnection(DatabaseConfig config) throws SQLException {
        return DriverManager.getConnection(
                config.getJdbcUrl(),
                config.getUsername(),
                config.getPassword()
        );
    }
}
